﻿using Bussiness;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using CCWin;

namespace Scada
{
    public partial class FrmDevTypeAdd : Skin_Mac
    {
        public string _DevTypeGuid;
        BLL _BLL = new BLL();
        public event ONBindDeviceTypeListHandler ONBindDeviceTypeList;
        public FrmDevTypeAdd()
        {
            InitializeComponent();
        }

        private void FrmDevTypeAdd_Load(object sender, EventArgs e)
        {
            DeviceType deviceType = _BLL._DAL.GetDeviceType(_DevTypeGuid);
            if (deviceType != null)
            {
                this.txt设备类型编号.Text = deviceType.DeviceTypeCode;
                this.txt设备类型名称.Text = deviceType.DeviceTypeName;
                this.txt采集频率.Text = deviceType.CaptureFreq.ToString2();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string devTypeCode = this.txt设备类型编号.Text.Trim();
            string devTypeName = this.txt设备类型名称.Text.Trim();
            string captureFreq = this.txt采集频率.Text.Trim();
            if (string.IsNullOrEmpty(devTypeCode))
            {
                MessageBox.Show("设备类型编号空！");
                return;
            }

            if (string.IsNullOrEmpty(devTypeName))
            {
                MessageBox.Show("设备类型名称空！");
                return;
            }

            DeviceType deviceType = new DeviceType();
            deviceType.Guid = string.IsNullOrEmpty(_DevTypeGuid) ? Guid.NewGuid().ToString() : _DevTypeGuid;
            deviceType.DeviceTypeCode = devTypeCode;
            deviceType.DeviceTypeName = devTypeName;
            deviceType.CaptureFreq = captureFreq.ToInt();
            Result result = _BLL._DAL.SaveDeviceType(deviceType, string.IsNullOrEmpty(_DevTypeGuid) ? true : false);
            if (result.Success)
            {
                MessageBox.Show("成功！");
                ONBindDeviceTypeList();
                this.Close();
            }
            else
            {
                MessageBox.Show("失败！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
